
package com.horizon.mobile.util;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.manager.HttpManager;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.model.Parameter;
import zuo.biao.library.util.MD5Util;
import zuo.biao.library.util.StringUtil;

import com.alibaba.fastjson.JSONObject;
import com.horizon.mobile.application.HzoaApplication;
import com.horizon.mobile.manage.SharePreferences;

/**
 * HTTP请求工具类
 *
 * @author 左大明
 * @use 添加请求方法xxxMethod >> HttpRequest.xxxMethod(...)
 * @must 所有请求的url、请求方法(GET, POST等)、请求参数(key-value方式，必要key一定要加，没提供的key不要加，value要符合指定范围)
 * 都要符合后端给的接口文档
 */
public class HttpRequest {
//	private static final String TAG = "HttpRequest";


    /**
     * 添加请求参数，value为空时不添加
     *
     * @param list
     * @param key
     * @param value
     */
    public static void addExistParameter(List<Parameter> list, String key, Object value) {
        if (list == null) {
            list = new ArrayList<Parameter>();
        }
        if (StringUtil.isNotEmpty(key, true) && StringUtil.isNotEmpty(value, true)) {
            list.add(new Parameter(key, value));
        }
    }


    /**
     * 基础URL，这里服务器设置可切换
     */
//	public static final String getBaseUrl()="http://192.168.1.108:8080/horizon-web/horizon/workflow/rest/";
    //public static final String getBaseUrl() = SettingUtil.getCurrentServerAddress(HzoaApplication.getInstance());
    public static final String KEY_PAGE_NUM = "pageNum";


    //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //user<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    public static final String KEY_RANGE = "range";

    public static final String KEY_ID = "id";
    public static final String KEY_USER_ID = "userId";
    public static final String KEY_CURRENT_USER_ID = "currentUserId";

    public static final String KEY_NAME = "name";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_AUTH_CODE = "authCode";

    public static final String KEY_SEX = "sex";
    public static final int SEX_MAIL = 1;
    public static final int SEX_FEMAIL = 2;
    public static final int SEX_ALL = 3;

    //account<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 注册
     *
     * @param phone
     * @param password
     * @param listener
     */
    public static void register(final String phone, final String password,
                                final int requestCode, final OnHttpResponseListener listener) {
        List<Parameter> paramList = new ArrayList<Parameter>();
        addExistParameter(paramList, KEY_PHONE, phone);
        addExistParameter(paramList, KEY_PASSWORD, MD5Util.MD5(password));

//		HttpManager.getInstance().post(paramList, getBaseUrl() + "user/register", requestCode, listener);
    }


    public static String getBaseUrl() {
        if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getIsNeiwang()) {
            if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrl("").length() > 0) {
                return "http://" + SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrl("") + "/horizon/workflow/rest/";
            }
        } else {
            if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrlWW("").length() > 0) {
                return "http://" + SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrlWW("") + "/horizon/workflow/rest/";
            }
        }

        return "http://192.168.1.108:8080/horizon-web/horizon/workflow/rest/";
    }

    public static String getWebBaseUrl() {
        if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getIsNeiwang()) {
            if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrl("").length() > 0) {
                return "http://" + SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrl("") + "";
            }
        }else {
            if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrlWW("").length() > 0) {
                return "http://" + SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getServerUrlWW("") + "";
            }
        }


        return "http://192.168.1.108:8080/horizon-web";
    }

    public static String getSuffix() {
        if (SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getSuffix(".wf").length() > 0) {
            return SharePreferences.getInstance(HzoaApplication.getInstance().getApplicationContext()).getSuffix(".wf");
        }

        return "http://192.168.1.108:8080/horizon-web";
    }

    public static void connectTest(final String url, String suffix, final int requestCode, final OnHttpResponseListener listener) {
        HttpManager.getInstance().post("", url + "/horizon/workflow/rest/testConnection" + suffix, requestCode, listener);
    }


    public static void searchUser(String accessToken, String searchUser, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("searchUser", searchUser);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "searchUser" + getSuffix(), requestCode, listener);
    }

    /**
     * 登陆
     *
     * @param phone
     * @param password
     * @param listener
     */
    public static void login(final String phone, final String password,
                             final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("loginName", phone);
        jobj.put(KEY_PASSWORD, password);
//		jobj.put("tenantCode", "hh");
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "user/project/login" + getSuffix(), requestCode, listener);
    }

    //account>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    /**
     * 获取用户
     *
     * @param userId
     * @param requestCode
     * @param listener
     */
    public static void getUser(long userId, final int requestCode, final OnHttpResponseListener listener) {
        List<Parameter> paramList = new ArrayList<Parameter>();
//		addExistParameter(paramList, KEY_CURRENT_USER_ID, HzoaApplication.getInstance().getCurrentUserId());
        addExistParameter(paramList, KEY_USER_ID, userId);

        HttpManager.getInstance().get(paramList, getBaseUrl() + "user/information", requestCode, listener);
    }

    public static final int RESULT_GET_USER_SUCCEED = 100;

    public static final int USER_LIST_RANGE_ALL = 0;
    public static final int USER_LIST_RANGE_RECOMMEND = 1;

    /**
     * 获取用户列表
     *
     * @param range
     * @param pageNum
     * @param requestCode
     * @param listener
     */
    public static void getUserList(int range, int pageNum, final int requestCode, final OnHttpResponseListener listener) {
        List<Parameter> paramList = new ArrayList<Parameter>();
//		addExistParameter(paramList, KEY_CURRENT_USER_ID, HzoaApplication.getInstance().getCurrentUserId());
        addExistParameter(paramList, KEY_RANGE, range);
        addExistParameter(paramList, KEY_PAGE_NUM, pageNum);

        HttpManager.getInstance().get(paramList, getBaseUrl() + "user/list", requestCode, listener);
    }

    public static final int RESULT_GET_USER_LIST_SUCCEED = 110;

    public static void getTodoList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "todo/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getNewFlowList(String accessToken, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "flow/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getNewFlowLisPlatform(String accessToken, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "startflowList" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getPersionAgentList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/" + requestType + "/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getHandWorkList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getReceiveList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/receive/manage/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getAgentPersionList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/person/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getAgentsList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getFlowTransferList(String accessToken, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/flow/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getMatterTransferList(String accessToken, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        if (search.length() > 0)
            jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/item/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getAgentDetailList(String accessToken, String dataId, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("dataId", dataId);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/dealinfo" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void reciveAgent(String accessToken, String id, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("dataId", id);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/handwork/receivework" + getSuffix() + "?", requestCode, listener);
    }


    public static void agentOperate(String accessToken, String ids, String agentId, String agentName, String startDate, String endDate, String moveTodo, String noticeType, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("id", ids);
        jobj.put("agentId", agentId);
        jobj.put("agentName", agentName);
        jobj.put("startDate", startDate);
        jobj.put("endDate", endDate);
        jobj.put("moveTodo", moveTodo);
        jobj.put("noticeType", noticeType);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/operate/action" + getSuffix() + "?", requestCode, listener);
    }

    //流程移交
    public static void flowTransfer(String accessToken, String receiveUserId, String receiveUserName, String flowId, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("receiveUserId", receiveUserId);
        jobj.put("receiveUserName", receiveUserName);
        jobj.put("flowId", flowId);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/flow/action" + getSuffix() + "?", requestCode, listener);
    }

    //事项移交
    public static void matterTransfer(String accessToken, String receiveUserId, String receiveUserName, String dataId, String trackId, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("receiveUserId", receiveUserId);
        jobj.put("receiveUserName", receiveUserName);
        jobj.put("dataId", dataId);
        jobj.put("trackId", trackId);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/item/action" + getSuffix() + "?", requestCode, listener);
    }

    //重新移交、取消移交
    public static void refuseOrReciveHandWord(String accessToken, String id, String requestType, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("dataId", id);
        jobj.put("requestType", requestType);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/manage/action" + getSuffix() + "?", requestCode, listener);
    }

    //重新移交、取消移交
    public static void reOrcanlHandWord(String accessToken, String id, String requestType, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("dataId", id);
        jobj.put("requestType", requestType);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "handwork/operate/action" + getSuffix() + "?", requestCode, listener);
    }

    public static void removeAgentSet(String accessToken, String id, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("id", id);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "agent/cancel/action" + getSuffix() + "?", requestCode, listener);
    }

    public static void getHasTodoList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "haddone/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getMethodList(String method, String accessToken, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + method + "/page" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getNoticeList(String accessToken, String requestType, String search, int pageNum, int pageSize, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("requestType", requestType);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "infoList" + getSuffix() + "?page=" + pageNum + "&limit=" + pageSize, requestCode, listener);
    }

    public static void getAddressBookList(String accessToken, String search, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        jobj.put("search", search);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "addressbook" + getSuffix(), requestCode, listener);
    }
    public static void getBusinessList(String accessToken, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "appmobilelist" + getSuffix() + "?", requestCode, listener);
    }
    public static void getNoRead(String accessToken, final int requestCode, final OnHttpResponseListener listener) {
        JSONObject jobj = new JSONObject();
        jobj.put("accessToken", accessToken);
        HttpManager.getInstance().post(jobj.toJSONString(), getBaseUrl() + "unread" + getSuffix() + "?", requestCode, listener);
    }
}